package com.kaka.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kaka.pojo.Permission;
import com.kaka.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("/findAll")
    public String findAll(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                          @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                          HttpServletRequest request) {
        PageHelper.startPage(page, limit);
        List<Permission> permissionList = permissionService.findAll();
        PageInfo<Permission> pageInfo = new PageInfo<>(permissionList);
        request.setAttribute("pageInfo", pageInfo);
        return "/permission/permission-list.jsp";
    }

    @GetMapping("/findById")
    public String findById(@RequestParam("id") int id, HttpServletRequest request) {
        Permission permission = permissionService.findById(id);
        System.out.println(permission);
        request.setAttribute("permission", permission);
        return "/permission/permission-edit.jsp";
    }

    @GetMapping("/edit")
    public String edit(Permission permission) {
        int editNum = permissionService.edit(permission);
        return "redirect:findAll";
    }

    @PostMapping("/add")
    public String add(Permission permission) {
        int addNum = permissionService.addPermission(permission);
        return "redirect:findAll";
    }

    @GetMapping("/deleteById")
    public String deleteById(Integer id) {
        int deleteNum = permissionService.deletePermission(id);
        return "redirect:findAll";
    }
}
